package org.bovver.restful;

import org.bovver.model.Product;
import org.bovver.model.User;
import org.bovver.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * Created by Paul on 2014/11/26.
 *
 */
@RestController
@RequestMapping(value = "/products"/*, consumes = "application/json", headers = "version=1.0"*/)
public class ProductRestController {
    private ProductService productService;

    @Autowired
    public void setProductService(ProductService productService) {
        this.productService = productService;
    }

    /**
     * 获取更多产品信息
     * @param last 已经加载数据的最小id
     * @param size 获取记录数
     * @return 获取到的数据
     */
    @RequestMapping(value = {"/more{size:[0-9]*}/{last:[0-9]*}", "/more{size:[0-9]*}{last}"} )
    public List<Product> more(@PathVariable("last") Long last, @PathVariable("size") Integer size) {
        long lastId = last == null || last == 0 ? Long.MAX_VALUE : last;
        int pageSize = size == null || size == 0 ? 20 : size;
        return productService.getMoreProducts(lastId, pageSize);
    }

    @RequestMapping(value = "/{id:[0-9]+}", method = RequestMethod.GET)
    public Product getProductById(@PathVariable("id") Long id) {

        return productService.getProduct(id);
    }

    @RequestMapping(value = "/{id:[0-9]+}", method = RequestMethod.POST)
    public Product update(Product model, @PathVariable("id") String id) {
        return null;
    }

    /**
     * 获取产品列表（默认只显示最新50条）
     * @return
     */
    @RequestMapping("")
    public List<Product> getProducts() {

        return this.more(Long.MAX_VALUE, 50);
    }




}
